package com.oa.pm.controller;

import com.joneying.common.web.response.Resp;
import com.oa.core.utils.request.RFormParam;
import com.oa.pm.dto.InductionTrainingDTO;
import com.oa.pm.entity.InductionTraining;
import com.oa.pm.service.IInductionTrainingService;
import com.oa.pm.vo.InductionTrainingDetailsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/inductionTraining")
@Api(description = "入职培训表模块")
public class InductionTrainingController extends BaseController {

    private final IInductionTrainingService inductionTrainingService;

    @Autowired
    public InductionTrainingController(IInductionTrainingService inductionTrainingService) {
        this.inductionTrainingService = inductionTrainingService;
    }

    @GetMapping("/list")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询所有")
    public Resp findInductionTrainingList(HttpServletRequest request){
        return new Resp().success(inductionTrainingService.findInductionTrainingList(new RFormParam(request)));
    }

    @GetMapping("/export")
    @CommonController
    @Description("导出")
    @ApiOperation(value = "导出")
    public void export(HttpServletRequest request, HttpServletResponse response){
        inductionTrainingService.export(new RFormParam(request), response);
    }

    @GetMapping("/{id}")
    @CommonController
    @Description("根据ID查询")
    @ApiOperation(value = "根据ID查询")
    public Resp<InductionTrainingDetailsVO> findInductionTrainingById(@ApiParam("入职培训ID") @PathVariable String id){
        return new Resp().success(inductionTrainingService.findInductionTrainingById(id));
    }

    @PostMapping("")
    @CommonController
    @Description("添加入职培训")
    @ApiOperation(value = "添加入职培训")
    public Resp addInductionTraining(@RequestBody InductionTrainingDTO inductionTrainingDTO){
        if(inductionTrainingService.addInductionTraining(inductionTrainingDTO)){
            return new Resp().success();
        }
        return new Resp().failure();
    }

    @PutMapping("")
    @CommonController
    @Description("修改入职培训")
    @ApiOperation(value = "修改入职培训")
    public Resp editInductionTraining(@RequestBody InductionTrainingDTO inductionTrainingDTO){
        if(inductionTrainingService.editInductionTraining(inductionTrainingDTO)){
            return new Resp().success();
        }
        return new Resp().failure();
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        InductionTraining inductionTraining = inductionTrainingService.selectById(id);
        if (inductionTraining != null && inductionTraining.getIsDeleted() == 2) {
            inductionTraining.setIsDeleted(1);
            if (inductionTrainingService.updateById(inductionTraining)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
